// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_main");
	var dialog = require("dialog");
	var validate = require("validate");
	var constant = require("constant");
	var databind = require("databind");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var noticeState = 1;
	var init = function(top, left) {
		footer.init();
		$("#footer").removeClass("none");

		var left = 0;
		if(base.getUrlParam("left")) {
			left = new Number(base.getUrlParam("left")) - 1;
		}
		if(base.getUrlParam("noticeState")) {
			noticeState = base.getUrlParam("noticeState");
		}
		leftMenu.init(0);
		topMenu.init(0);

		$(".table-section").removeClass("none");
		$("#leftMenu1").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu1 .leftMenu li:first-child").addClass("leftMenu-open");
		$("#leftMenu1").find(".leftMenu-item").eq(left).addClass("leftMenu-active");
		inquireRecieveNoticeLogisticsPageList();
		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if(base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		/*新增的点击事件*/
		$("#leftMenu1").on("click", "#undoneTaskList", function() {
			location.href = "../newTaskList/newTaskList.html?noticeState=2&left=2";
		});

		$("#leftMenu1").on("click", "#doneTaskList", function() {
			location.href = "../newTaskList/newTaskList.html?noticeState=3&left=3";
		});

		$("#addTallyTask").unbind("click").click(function() {
			router.route("newTaskList/createTask.html");
		});
	
	};
	  //删除
    var deleteReceivingTally = function(recieveNoticeId){
//    	var delete_clientManage_dialog = $("#delete_clientManage_dialog");
//    	dialog.showDialog(delete_clientManage_dialog, function() {});
//    	$(".footerBtn").on("click", "#sure_delete", function() {
    	base.fnAjax({
            action: "deleteReceivingTally.json",
            oData: {
            	recieveNoticeId : recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	dialog.hideDialog($("#delete_clientManage_dialog"));
                	base.alert(1, "删除成功！");
                	inquireRecieveNoticeLogisticsPageList();
                } else {
                    base.alert(2,"删除失败！");
                }
            }
        });
//    	});
    }
	//查询收货通知列表
	var inquireRecieveNoticeLogisticsPageList = function() {
		//var body = base.parseForm($(".page-content-search"));
		base.fnAjax({
			action: "inquireRecieveNoticeLogisticsPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				noticeState: noticeState
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#logisticsCompanyListTpl").html();
					var html = juicer(tpl, data);
					$("#newTaskListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireRecieveNoticeLogisticsPageList();
						});
					//详情
					$(".handle-type").unbind("click").click(function() {
						var recieveNoticeId = [];
						var $this = $(this).parents("tr.tr_style");
						recieveNoticeId.push($this.attr("recieveNoticeId"));
						router.route("newTaskList/newTaskDetail.html?recieveNoticeId=" + recieveNoticeId + "&noticeState=" + noticeState, true);
					});
					
					$("#newTaskListBody").on("click",".delete-type",function(){
//			        	$("#popdialog").html(html_delete);
			           var recieveNoticeId = [];
			           var $this = $(this).parents("tr.tr_style");
			           recieveNoticeId.push($this.attr("recieveNoticeId"));
			           deleteReceivingTally(recieveNoticeId);
			        });
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
	return {
		init: init
	};
});